package Lee_code;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;

/**
 * @author blh
 * @date 2024年07月08日 14:39
 */
public class Lee_445_78 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        Deque<Integer> dequeL1 = new LinkedList<>();
        Deque<Integer> dequeL2 = new LinkedList<>();

        ListNode temp;
        temp = l1;
        while (temp != null) {
            dequeL1.push(temp.val);
            temp = temp.next;
        }

        temp = l2;
        while (temp != null) {
            dequeL2.push(temp.val);
            temp = temp.next;
        }
        if (dequeL1.size() > dequeL2.size()) {
            for (int i = 0; i < (dequeL1.size() - dequeL2.size()); i++) {
                dequeL2.addLast(0);
            }
        } else {
            for (int i = 0; i < (dequeL2.size() - dequeL1.size()); i++) {
                dequeL1.addLast(0);
            }
        }
        System.out.println(dequeL1.size());
        System.out.println(dequeL2.size());
        int sign = 0;
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = null;
        while (!dequeL1.isEmpty()){
            int temp1 = dequeL1.pop();
            int temp2 = dequeL2.pop();
            int tempSum = temp1 + temp2 + sign;
            if (tempSum >= 10) {
                tempSum = tempSum - 10;
                sign = 1;
            } else {
                sign = 0;
            }
            ListNode tempNode = new ListNode(tempSum);
            tempNode.next = dummyHead.next;
            dummyHead.next = tempNode;
        }
        return dummyHead.next;
    }
}
